//
//  NSString+Category.m
//  GoodsManagement
//
//  Created by 车浩 on 2017/3/7.
//  Copyright © 2017年 CheHao. All rights reserved.
//

#import "NSString+Category.h"

@implementation NSString (Category)

- (id)MD5
{
    const char *cStr = [self UTF8String];
    unsigned char digest[16];
    CC_MD5( cStr, (CC_LONG)strlen(cStr), digest ); // This is the md5 call
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return  output;
}


//文本高度
+ (CGFloat)heightForString:(NSString *)str fontSize:(NSInteger)fontSize width:(CGFloat)width{
    //1获取文本属性 （字体样式，字体大小，行高）用字典储存  key值为NSFontAttributeName
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:fontSize], NSFontAttributeName, nil];
    //    2根据一个字符串绘制一个矩形
    CGRect bound = [str boundingRectWithSize:(CGSizeMake(width, 1000)) options:(NSStringDrawingTruncatesLastVisibleLine |
                                                                                NSStringDrawingUsesLineFragmentOrigin |
                                                                                NSStringDrawingUsesFontLeading) attributes:dic context:nil];
    return bound.size.height;
    
}

//文本宽度
+ (CGFloat)widthForString:(NSString *)str fontSize:(NSInteger)fontSize height:(CGFloat)height{
    //1获取文本属性 （字体样式，字体大小，行高）用字典储存  key值为NSFontAttributeName
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:fontSize], NSFontAttributeName, nil];
    //    2根据一个字符串绘制一个矩形
    CGRect bound = [str boundingRectWithSize:(CGSizeMake(100000, height)) options:(NSStringDrawingTruncatesLastVisibleLine |
                                                                                   NSStringDrawingUsesLineFragmentOrigin |
                                                                                   NSStringDrawingUsesFontLeading) attributes:dic context:nil];
    return bound.size.width;
    
}

+ (BOOL)isPhone:(NSString *)str{
    
    NSString * regex = @"^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    
    BOOL isMatch = [pred evaluateWithObject:str];
    
    if (!isMatch) {
        
        //        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的手机号码" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
        //
        //        [alert show];
        //
//        [ShowMessage showCenterViewMessage:@"请输入正确的手机号码"];
        return NO;
        
    }
    
    
    return YES;
}

+ (BOOL)isNumLetterAndChinese:(NSString *)string showMessage:(NSString *)showMessage{
    
    NSString *regex = @"[a-zA-Z\u4e00-\u9fa5][a-zA-Z0-9\u4e00-\u9fa5]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    BOOL isTrue = ![pred evaluateWithObject: string];
    
    if (isTrue) {
//        [ShowMessage showCenterViewMessage:showMessage];
    }
    
    return isTrue;
    
}

+ (BOOL)isMail:(NSString *)string{
    
    
    NSString *regex = @"[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    BOOL returnB = ![pred evaluateWithObject: string];
    
    if (returnB) {
//        [ShowMessage showCenterViewMessage:@"请填写正确的邮箱"];
    }
    
    return returnB;
    
}

+ (BOOL)isPwd:(NSString *)string{
    
    NSString *regex = @"[a-zA-Z0-9][a-zA-Z0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    BOOL returnB = ![pred evaluateWithObject: string];
    
    if (returnB) {
//        [ShowMessage showCenterViewMessage:@"密码只能由数字、字母组成"];
    }
    
    return returnB;
}


+ (BOOL)passWordTextFileisExistSpace:(NSString *)str{
    
    NSRange aa = [str rangeOfString:@" "];
    
    if (aa.location == NSNotFound) {
        return NO;
    }else{
        return YES;
    }
}

@end
